package com.like.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.like.entity.InfoListResp;
import com.like.entity.M;
import com.like.hospitalworld.R;
import com.like.utils.ImageUtils;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

public class InfoAdapter extends BaseAdapter {

    public static final int TYPE_TOP = 0;
    public static final int TYPE_MEETINGS = 1;

    private List<InfoListResp.InformationBase> mInfos;
    private M m;

    public InfoAdapter(List<InfoListResp.InformationBase> infos, M m) {
        this.mInfos = infos;
        this.m = m;
    }

    public void update(List<InfoListResp.InformationBase> infos, boolean append) {
        if (append)
            this.mInfos.addAll(infos);
        else
            this.mInfos = infos;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mInfos.size() + 1;
    }

    @Override
    public InfoListResp.InformationBase getItem(int position) {
        if (position == 0)
            return null;
        return mInfos.get(position - 1);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE_TOP;
        return TYPE_MEETINGS;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MeetingViewHolder mvh = null;
        TopViewHolder tvh = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case TYPE_TOP:
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_meeting_top, parent, false);
                    tvh = new TopViewHolder(convertView);
                    convertView.setTag(tvh);
                    break;
                case TYPE_MEETINGS:
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_meeting, parent, false);
                    mvh = new MeetingViewHolder(convertView);
                    convertView.setTag(mvh);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_TOP:
                    tvh = (TopViewHolder) convertView.getTag();
                    break;
                case TYPE_MEETINGS:
                    mvh = (MeetingViewHolder) convertView.getTag();
                    break;
            }
        }
        switch (type) {
            case TYPE_TOP:
                break;
            case TYPE_MEETINGS:
                InfoListResp.InformationBase info = getItem(position);
                ImageUtils.showImage(m.mContext, info.titlePage, m.getToken(), mvh.sPreviewImg);
                mvh.sTitle.setText(info.titleName);
                mvh.sContent.setText(info.summary);
                break;
        }
        return convertView;
    }

    public class MeetingViewHolder {

        @ViewInject(R.id.preview_img)
        ImageView sPreviewImg;
        @ViewInject(R.id.title)
        TextView sTitle;
        @ViewInject(R.id.content)
        TextView sContent;

        public MeetingViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }

    }

    public class TopViewHolder {
        public TopViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }
    }
}
